<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加商品</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="add">
      <el-alert
        title="注意：只允许为第三级分类设置相关参数！"
        type="warning"
        show-icon
      >
      </el-alert>
      <span>选择商品分类：</span>
      <el-cascader
        size="large"
        v-model="value"
        :options="options"
        :props="{
          expandTrigger: 'hover',
          label: 'cat_name',
          value: 'cat_id',
        }"
        @change="handleChange"
      ></el-cascader>
      <el-tabs
        :tab-position="tabPosition"
        :before-leave="beforeLeave"
        @tab-click="handle"
      >
        <el-tab-pane label="动态参数">
          <el-button type="primary" :disabled="value.length !== 3" @click="dong"
            >添加参数</el-button
          >
          <el-table
            border
            ref="singleTable"
            :data="tableData"
            highlight-current-row
            style="width: 100%"
          >
            <el-table-column type="expand">
              <template slot-scope="{ row }">
                <el-tag
                  :key="tag.attr_id"
                  v-for="tag in row.arr"
                  closable
                  :disable-transitions="false"
                  @close="handleClose(tag, row)"
                >
                  {{ tag }}
                </el-tag>
                <el-input
                  class="input-new-tag"
                  v-if="row.inputVisible"
                  v-model="row.inputValue"
                  ref="saveTagInput"
                  size="small"
                  @keyup.enter.native="handleInputConfirm(row)"
                  @blur="handleInputConfirm(row)"
                >
                </el-input>
                <el-button
                  v-else
                  class="button-new-tag"
                  size="small"
                  @click="showInput(row)"
                  >+ New Tag</el-button
                >
              </template>
            </el-table-column>
            <el-table-column
              type="index"
              width="50"
              align="center"
            ></el-table-column>
            <el-table-column label="参数名称" prop="attr_name">
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="{ row }">
                <el-button
                  type="primary"
                  icon="el-icon-edit"
                  @click="editDong(row)"
                  >编辑</el-button
                >
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  @click="open(row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="静态属性">
          <el-button type="primary" :disabled="value.length !== 3" @click="jing"
            >添加属性</el-button
          >
          <el-table
            border
            ref="singleTable"
            :data="tableData"
            highlight-current-row
            style="width: 100%"
          >
            <el-table-column type="expand">
              <template slot-scope="{ row }">
                <el-tag
                  :key="tag.attr_id"
                  v-for="tag in row.arr"
                  closable
                  :disable-transitions="false"
                  @close="handleClose(tag, row)"
                >
                  {{ tag }}
                </el-tag>
                <el-input
                  class="input-new-tag"
                  v-if="row.inputVisible"
                  v-model="row.inputValue"
                  ref="saveTagInput"
                  size="small"
                  @keyup.enter.native="handleInputConfirm(row)"
                  @blur="handleInputConfirm(row)"
                >
                </el-input>
                <el-button
                  v-else
                  class="button-new-tag"
                  size="small"
                  @click="showInput(row)"
                  >+ New Tag</el-button
                >
              </template>
            </el-table-column>
            <el-table-column
              type="index"
              width="50"
              align="center"
            ></el-table-column>
            <el-table-column label="参数名称" prop="attr_name">
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="{ row }">
                <el-button
                  type="primary"
                  icon="el-icon-edit"
                  @click="editJing(row)"
                  >编辑</el-button
                >
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  @click="open(row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </div>
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules">
        <el-form-item prop="name" :label="label">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { categoriesApi, attributesApi, TagApi, delCategories, addCategories, editCategories } from '@/api/params.js'
export default {
  name: '',
  data () {
    return {
      attrId: '',
      label: '',
      title: '',
      activeName: '',
      tableData: [],
      options: [],
      value: [],
      tabPosition: 'top',
      ruleForm: {
        goods_cat: ''
      },
      dialogFormVisible: false,
      form: {
        name: ''
      },
      rules: {
        name: [
          { trigger: 'blur', required: true, message: '请输入参数名称' }
        ]
      }
    }
  },
  async created () {
    const res = await categoriesApi()
    this.options = res.data
  },
  computed: {
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('submit!')
        } else {
          console.log('error submit!!')
          return false
        }
      })
      // console.log(this.$refs[formName].validate())
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    },
    async beforeLeave (activeName, oldActiveName) {
      this.activeName = activeName
    },
    async handleChange (value) {
      console.log(this.value)
      this.msg = value[value.length - 1]
      const res = await attributesApi(this.msg, 'many')
      this.flag = false
      res.data.forEach(item => {
        item.inputVisible = false
        item.inputValue = ''
        item.arr = item.attr_vals.split(item.attr_vals.includes(',') ? ',' : ' ')
      })
      this.tableData = res.data
    },
    async handle () {
      try {
        if (this.activeName === '0') {
          const res = await attributesApi(this.msg, 'many')
          res.data.forEach(item => {
            item.inputVisible = false
            item.inputValue = ''
            item.arr = item.attr_vals.split(item.attr_vals.includes(',') ? ',' : ' ')
          })
          this.tableData = res.data
        } else {
          const res = await attributesApi(this.msg, 'only')
          res.data.forEach(item => {
            item.inputVisible = false
            item.inputValue = ''
            item.arr = item.attr_vals.split(item.attr_vals.includes(',') ? ',' : ' ')
          })
          this.tableData = res.data
        }
      } catch { }
    },
    handleClose (tag, row) {
      row.arr.splice(row.arr.indexOf(tag), 1)
      console.log(row.arr.join())
      const obj = {
        attr_name: row.attr_name,
        attr_sel: 'many',
        attr_vals: row.arr.join()
      }
      TagApi(this.msg, row.attr_id, obj)
    },
    showInput (row) {
      row.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },
    handleInputConfirm (row) {
      const inputValue = row.inputValue
      if (inputValue) {
        row.arr.push(inputValue)
        const obj = {
          attr_name: row.attr_name,
          attr_sel: 'many',
          attr_vals: row.arr.join()
        }
        TagApi(this.msg, row.attr_id, obj)
      }
      row.inputVisible = false
      row.inputValue = ''
    },
    open (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await delCategories(this.value[this.value.length - 1], row.attr_id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        if (this.activeName === '1') {
          const res = await attributesApi(this.msg, 'only')
          res.data.forEach(item => {
            item.inputVisible = false
            item.inputValue = ''
            item.arr = item.attr_vals.split(item.attr_vals.includes(',') ? ',' : ' ')
          })
          this.tableData = res.data
        } else {
          const res = await attributesApi(this.msg, 'many')
          res.data.forEach(item => {
            item.inputVisible = false
            item.inputValue = ''
            item.arr = item.attr_vals.split(item.attr_vals.includes(',') ? ',' : ' ')
          })
          this.tableData = res.data
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    dong () {
      this.form.name = ''
      this.dialogFormVisible = true
      this.title = '添加动态参数'
      this.label = '动态参数'
    },
    jing () {
      this.form.name = ''
      this.dialogFormVisible = true
      this.title = '添加静态属性'
      this.label = '静态属性'
    },
    async add () {
      this.dialogFormVisible = false
      if (this.title === '添加动态参数') {
        const obj = {
          attr_name: this.form.name,
          attr_sel: 'many'
        }
        await addCategories(this.msg, obj)
        const res = await attributesApi(this.msg, 'many')
        res.data.forEach(item => {
          item.inputVisible = false
          item.inputValue = ''
          item.arr = item.attr_vals.split(item.attr_vals.includes(',') ? ',' : ' ')
        })
        this.tableData = res.data
      } else if (this.title === '添加静态属性') {
        const obj = {
          attr_name: this.form.name,
          attr_sel: 'only'
        }
        await addCategories(this.msg, obj)
        const res = await attributesApi(this.msg, 'only')
        res.data.forEach(item => {
          item.inputVisible = false
          item.inputValue = ''
          item.arr = item.attr_vals.split(item.attr_vals.includes(',') ? ',' : ' ')
        })
        this.tableData = res.data
      } else if (this.title === '修改动态参数') {
        const obj = {
          attr_name: this.form.name,
          attr_sel: 'many'
        }
        await editCategories(this.msg, this.attrId, obj)
        const res = await attributesApi(this.msg, 'many')
        res.data.forEach(item => {
          item.inputVisible = false
          item.inputValue = ''
          item.arr = item.attr_vals.split(item.attr_vals.includes(',') ? ',' : ' ')
        })
        this.tableData = res.data
      } else if (this.title === '修改静态属性') {
        const obj = {
          attr_name: this.form.name,
          attr_sel: 'only'
        }
        await editCategories(this.msg, this.attrId, obj)
        const res = await attributesApi(this.msg, 'only')
        res.data.forEach(item => {
          item.inputVisible = false
          item.inputValue = ''
          item.arr = item.attr_vals.split(item.attr_vals.includes(',') ? ',' : ' ')
        })
        this.tableData = res.data
      }
    },
    editDong (row) {
      this.dialogFormVisible = true
      this.form.name = row.attr_name
      this.attrId = row.attr_id
      this.title = '修改动态参数'
      this.label = '动态参数'
    },
    editJing (row) {
      this.dialogFormVisible = true
      this.form.name = row.attr_name
      this.attrId = row.attr_id
      this.title = '修改静态属性'
      this.label = '静态属性'
    }
  }
}
</script>
<style lang='less'  scoped>
.add {
  padding: 20px;
  background-color: #fff;
}
.el-steps {
  width: 1000px;
  margin-left: 20px;
  margin-bottom: 20px;
}
.el-alert {
  margin-bottom: 16px;
}
.el-table {
  margin-top: 10px;
}
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>